﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1.DAO;
using Manager;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace ClassLibrary1
{
    public class Business_User_Role
    {
        private DbManager db;

        public Business_User_Role()
        {
            db = new DbManager();
        }

        public DataTable getRoleWithoutAdmin()
        {
            DataTable resultGet = new DataTable();
            //resultGet = db.getData("SELECT dbo.Users.*, dbo.Roles.RoleName FROM dbo.Roles INNER JOIN dbo.Users ON dbo.Roles.RoleID = dbo.Users.IDRole WHERE NOT dbo.Roles.RoleName = 'admin'");
            resultGet = db.getData("SELECT * FROM Roles WHERE NOT RoleName = 'admin' and Status = 1");
            return resultGet;
        }

        public DataTable getRoleAll()
        {
            DataTable resultGet = new DataTable();
            resultGet = db.getData("SELECT * FROM Roles");
            return resultGet;
        }

        public bool addRole(string name)
        {
            //SqlParameter[] param = { new SqlParameter("a", value), new SqlParameter("b", value) };
            SqlParameter param = new SqlParameter("name", name);
            try
            {
                db.changeData("Insert Into Roles (RoleName) Values (@name)", param);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                return false;
            }


        }

        public bool editRole(int id, string name)
        {
            SqlParameter[] param = {
                                       new SqlParameter("id", id),
                                       new SqlParameter("name", name)
                                   };
            try
            {
                db.changeData("Update Roles set RoleName = @name where RoleID=@id", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }



        }

        public bool deleteRole(int id)
        {
            try
            {
                SqlParameter param = new SqlParameter("id", id);
                db.changeData("Update Roles set Status = 0 where RoleID = @id", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }

        }

        public bool rollBackRole(int id)
        {
            try
            {
                SqlParameter param = new SqlParameter("id", id);
                db.changeData("Update Roles set Status = 1 where RoleID = @id", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        public DataTable getUserWithoutAdmin()
        {
            DataTable resultGet = new DataTable();
            resultGet = db.getData("SELECT dbo.Users.*, dbo.Roles.RoleName FROM dbo.Roles INNER JOIN dbo.Users ON dbo.Roles.RoleID = dbo.Users.IDRole WHERE NOT dbo.Roles.RoleName = 'admin' and dbo.Users.Status = 1");
            return resultGet;
        }
        public DataTable getUserAll()
        {
            DataTable resultGet = new DataTable();
            resultGet = db.getData("SELECT dbo.Users.*, dbo.Roles.RoleName FROM dbo.Roles INNER JOIN dbo.Users ON dbo.Roles.RoleID = dbo.Users.IDRole");
            return resultGet;
        }

        public bool addUser(UserDAO userDao)
        {

            SqlParameter[] param = {
                                         new SqlParameter("username",userDao.Username),
                                         new SqlParameter("password",userDao.Password), 
                                         new SqlParameter("firstname", userDao.FirstName), 
                                         new SqlParameter("lastname",userDao.LastName),
                                         new SqlParameter("identity",userDao.Identity),
                                         new SqlParameter("birthday",userDao.BirthDay),
                                         new SqlParameter("address",userDao.Address),
                                         new SqlParameter("phone",userDao.Phone), 
                                         new SqlParameter("email",userDao.Email),
                                         new SqlParameter("idRole",userDao.IDRole), 
                                   };
            try
            {
                db.changeData("Insert Into Users (Username, [Password], FirstName, LastName, [Identity], Birthday, [Address], Phone, Email, IDRole)" +
                          "values (@username, @password,@firstname, @lastname, @identity, @birthday, @address, @phone, @email, @idRole)", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
            
            
        }

        public bool editUser(UserDAO userDao)
        {
            SqlParameter[] param = {
                                         new SqlParameter("id",userDao.UserId),
                                         new SqlParameter("firstname", userDao.FirstName), 
                                         new SqlParameter("lastname",userDao.LastName),
                                         new SqlParameter("identity",userDao.Identity),
                                         new SqlParameter("birthday",userDao.BirthDay),
                                         new SqlParameter("address",userDao.Address),
                                         new SqlParameter("phone",userDao.Phone), 
                                         new SqlParameter("email",userDao.Email),
                                         new SqlParameter("idRole",userDao.IDRole), 
                                   };
            try
            {
                db.changeData("Update Users set FirstName = @firstname, LastName = @lastname," +
                              "[Identity] = @identity, Birthday = @birthday, [Address] = address," +
                              "Phone = @phone, Email = @email, IDRole = @idRole where UserID = @id", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
            return false;
        }

        public bool deleteUser(int id)
        {
            try
            {
                SqlParameter param = new SqlParameter("id", id);
                db.changeData("Update Users set Status = 0 where UserID = @id", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        public bool rollBackUser(int id)
        {
            try
            {
                SqlParameter param = new SqlParameter("id", id);
                db.changeData("Update Users set Status = 1 where UserID = @id", param);
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }
    }
}
